"use server"
import { searchQuestionVOByPage } from "@/api/question"
import QuestionTable from "@/components/question-table"
import "./index.css"

interface Props {
  searchParams: { q: string }
}

/**
 * 题目大全页面
 */
export default async function QuestionsPage({ searchParams }: Props) {
  const { q: searchText } = searchParams
  let questionList: QuestionVO[] = []
  let total = 0

  try {
    const res = await searchQuestionVOByPage({
      searchText,
      pageSize: 12,
      sortField: "createTime",
      sortOrder: "descend"
    })
    questionList = res.data.records ?? []
    total = res.data.total ?? 0
  } catch (error) {
    console.error("获取题目失败，" + error)
  }

  return (
    <div id="questions-page" className="max-width-content">
      <QuestionTable
        defaultQuestionList={questionList}
        defaultTotal={total}
        defaultSearchParams={{ title: searchText }}
      />
    </div>
  )
}
